package com.ophelisis.actions.system;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.ophelisis.common.util.DateUtil;
import com.ophelisis.persistence.dao.PortalUserDAO;
import com.ophelisis.persistence.domain.PortalUserVO;
 
/** <pre>
 * Class : com.apexcns.xdp.actions.system.Logout / Logout.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author hlee / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2013. 01. 17.
 */
@Controller
@SessionAttributes({"userId", "userNm", "userLevel", "userUrl", "menuGroupList", "menuList", "userAuthList"})
public class Logout {
	@Autowired
	PortalUserDAO userDAO;
	
	private Log log = LogFactory.getLog(this.getClass());
	private static final String _LogPrefixStr = "Logout Log> ";
	
	@RequestMapping(value="logout.action")
	public ModelAndView mainPage(
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		HttpSession session = request.getSession(false);
		
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
		log.debug(_LogPrefixStr + "|" + " Logout called !!!");
		log.debug(_LogPrefixStr + "|" + "------------------------------------------------------------------------------------");
	
		try {
			PortalUserVO userVO = new PortalUserVO();
			
			userVO.setUserId((String)session.getAttribute("userId"));
			userVO.setLogoutDatetime(DateUtil.nowDateString());
			
			try {
				userDAO.setPortalUserModify(userVO);
			} catch (SQLException e1) {
				log.error("DB 사용자 정보 업데이트 failed!, " + userVO.toString(), e1);
			} catch (DataAccessException e2) {
				log.error("DB 사용자 정보 업데이트 failed!, " + userVO.toString(), e2);
			} catch (Exception e3) {
				log.error("DB 사용자 정보 업데이트 failed!, " + userVO.toString(), e3);
			}

			mav.addObject("userId", "");
			mav.addObject("userNm", "");
			mav.addObject("userLevel", "");
			mav.addObject("userUrl", "");
			mav.addObject("menuGroupList", "");
			mav.addObject("menuList", "");
			mav.addObject("userAuthList", "");
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		mav.setViewName("/WEB-INF/jsp/system/logout_ok.jsp");
		return mav;
	} 
}
